package cn.itcast.listener;

import cn.itcast.utils.MailUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.IOException;
import java.util.Map;

public class DeliveryNoticeListener implements MessageListener {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onMessage(Message message) {
        try {
            byte[] body = message.getBody();
            Map map = objectMapper.readValue(body, Map.class);

            /*
            sendMsg(String to, String subject, String content)
                    to： 发送给谁（邮件的接受者）
                    subject: 邮件的主题
                    content: 邮件的正文内容
            */
            String email = ((String) map.get("email"));
            String contractNo = ((String) map.get("contractNo"));
            String userName = ((String) map.get("userName"));

            String subject = contractNo+"交货期限到期提醒";
            String content = userName+"用户,您签下的"+contractNo+"合同快到交货期限了请尽快处理,";

            MailUtil.sendMsg(email,subject,content);

          /*  System.out.println("发送邮件成功，进入10秒睡眠...");
            Thread.sleep(10000);*/
        }  catch (Exception e) {
            e.printStackTrace();
        }


    }
}
